package ex;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.navigator.menu.MenuRepository;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.guinea.core.acegi.AcegiCacheManager;
import com.guinea.core.service.CommonService;
import com.guinea.core.tag.DictEntry;
import com.guinea.core.utils.StringUtils;

import ex.system.service.DictionaryServiceEx;
import ex.system.service.MenuServiceEx;

public class ConsoleAction extends DispatchAction {
	AcegiCacheManager acegiCacheManager;
	MenuServiceEx menuServiceEx;
	CommonService commonService;
	DictionaryServiceEx dictionaryServiceEx;
	
	public AcegiCacheManager getAcegiCacheManager() {
		return acegiCacheManager;
	}

	public void setAcegiCacheManager(AcegiCacheManager acegiCacheManager) {
		this.acegiCacheManager = acegiCacheManager;
	}

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	public MenuServiceEx getMenuServiceEx() {
		return menuServiceEx;
	}

	public void setMenuServiceEx(MenuServiceEx menuServiceEx) {
		this.menuServiceEx = menuServiceEx;
	}

	public DictionaryServiceEx getDictionaryServiceEx() {
		return dictionaryServiceEx;
	}

	public void setDictionaryServiceEx(DictionaryServiceEx dictionaryServiceEx) {
		this.dictionaryServiceEx = dictionaryServiceEx;
	}

	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String module = request.getParameter("module");
		request.setAttribute("moduleName",module);
//		if (module != null) {
//			return new ActionForward("/WEB-INF/jsp/" + module + "/index.jsp");
//		}
		String refresh = request.getParameter("refresh");
		if(!StringUtils.isEmpty(refresh)){
			if(refresh.trim().equalsIgnoreCase("user") || refresh.trim().equalsIgnoreCase("resource")){
				acegiCacheManager.refreshCaches();
				
				ServletContext context = request.getSession().getServletContext();
				
				MenuRepository repository = menuServiceEx.getRepository();
				MenuRepository defaultRepository = (MenuRepository) context
						.getAttribute(MenuRepository.MENU_REPOSITORY_KEY);
				repository.setDisplayers(defaultRepository.getDisplayers());
				context.setAttribute("repository", repository);
				
				List dict = dictionaryServiceEx.get();
				context.setAttribute("dict", dict);
				
				
				List rsscs = commonService.getRsscs();
				context.setAttribute("rsscs", rsscs);
				List provinces = commonService.getProvinces();
				context.setAttribute("provinces", provinces);
				List cities = commonService.getCities();
				context.setAttribute("cities", cities);
				List districts = commonService.getDistricts();
				context.setAttribute("districts", districts);
				
				cities = DictEntry.assembleEntry(cities,districts);
				provinces = DictEntry.assembleEntry(provinces,cities);
				String pcdJson = DictEntry.convertEntryListToJson(provinces);;
				context.setAttribute("provincecitydistrict",pcdJson);
			}
		}
		
		return mapping.findForward("index");
	}
}
